//
//  DetailNewsViewController.m
//  小汽车
//
//  Created by 杜远洋 on 14-12-29.
//  Copyright (c) 2014年 蓝欧科技. All rights reserved.
//

#import "DetailNewsViewController.h"
#import "Masonry.h"
#import "CarNewsBase.h"
#import "CarNewsModel.h"
@interface DetailNewsViewController ()<UIWebViewDelegate>

@end

@implementation DetailNewsViewController
- (void)dealloc
{
    [_webSite release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createDetailPage];
    [self NotificationCenter];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)]autorelease];
}

- (void)createDetailPage
{
    UIWebView * webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    [self.view addSubview: webView];
    __block UIViewController *weakSelf = self;
    
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(weakSelf.view);
    }];
    webView.delegate = self;
    webView.backgroundColor = [UIColor whiteColor];
    NSURL *url = [NSURL URLWithString:self.webSite];
    [webView setScalesPageToFit:YES];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
  
    [webView release];
}
- (void)buttonAction:(id)sender
{
    NSString *str = [CarNewsBase insertIntoCarNewsDetail:[CarNewsModel carNewsWithNewsTitle:_newsTitle newsLink:_webSite]];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
}

#pragma mark - （详情）这个地方改不了北京颜色 再另想办法把

- (void)NotificationCenter
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNightOrDay:) name:@"changeState" object:nil];
}

- (void)changeNightOrDay:(NSNotification *)notification
{
    NSString *str = [notification.userInfo objectForKey:@"STATE"];
    if ([str isEqualToString:@"CHANGETONIGHT"]) {
        [self.view setBackgroundColor:[UIColor colorWithRed:54/255.0f green:54/255.0f blue:54/255.0f alpha:1]];
        
    }
    else
    {
        [self.view setBackgroundColor:[UIColor whiteColor]];
    }
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSLog(@"aaaaaa");
#pragma mark - 如果链接的网页上存才点击事件，那么在点击的时候取消他的响应（？疑问）
        return NO;
      
    }
    NSLog(@"bbbb");
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
